package com.hopu.v1.service.admin.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.v1.exception.BusinessException;
import com.hopu.v1.mapper.admin.system.OrganizationMapper;
import com.hopu.v1.pojo.system.Organization;
import com.hopu.v1.service.admin.system.OrganizationService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author YwaiX
 * @since 2024-09-12
 */
@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements OrganizationService {
    
    @Resource
    private OrganizationMapper mapper;

    /**
     * 获取组织列表
     * @return
     */
    @Override
    public List<Organization> getOrganizaList() {
        return mapper.getOrganizaList();
    }
    
    /**
     * 根据id获取组织信息
     *
     * @param id
     * @return
     */
    @Override
    public Organization getOrganizationById(Integer id) {
        return baseMapper.selectById(id);
    }
    
    /**
     * 添加组织
     *
     * @param organization
     */
    @Override
    public void insert(Organization organization) {
        QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organization_name", organization.getOrganizationName());
        
        if (baseMapper.selectOne(queryWrapper) != null) {
            throw new BusinessException("组织名称重复");
        }
        
        baseMapper.insert(organization);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        mapper.updateById(new Organization().setId(id).setStatus(status));
    }
    
    /**
     * 根据父级id查询所有子级id集合
     * @param pid
     * @return
     */
    @Override
    public List<Integer> getOrganizationIds(Integer pid) {
        return mapper.getOrganizationIds(pid);
    }
}
